package com.solvePlat.config;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

public class WebChatServer {

    private int port;
    public WebChatServer(int port){
        this.port = port;
    }

    public void start(){//开始

        //定义两个线程组

        EventLoopGroup boss = new NioEventLoopGroup();//负责新的客户端接入
        EventLoopGroup worker = new  NioEventLoopGroup();//负责处理客户端的网络请求
        try{

            ServerBootstrap  booStrap = new ServerBootstrap();
            booStrap.group(boss,worker)//绑定线程组
                    .channel(NioServerSocketChannel.class)//指定我的请求都是由这个类进行处理的
                    .childHandler(new WebChatServerInitize())
                    .option(ChannelOption.SO_BACKLOG,128)//定义配置参数
                    .childOption(ChannelOption.SO_KEEPALIVE,true);//保持连接

            ChannelFuture future = booStrap.bind(port).sync();

            System.out.println("服务端已经启动");

            future.channel().closeFuture().sync();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            boss.shutdownGracefully();
            worker.shutdownGracefully();
        }


    }

    public static void main(String[] args) {
        WebChatServer webChatServer = new WebChatServer(8080);
    }
}
